﻿/*************************************************************************
* 计算机名：AA-FA-05
* CLR 版本：4.0.30319.42000
* 命名空间: Unity
* 类    名：LogHelper
* 当 前 域：HYRC
* 创 建 人：dailuyu
* 创建时间：2021/6/11 11:42:26
* 描    述：
*  V0.01 2021 初版
*
*  Copyright (c) 2021 aatsr Corporation. All rights reserved.
*  ┌──────────────────────────────────┐
*  │　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*  └──────────────────────────────────┘
***************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Unity
{
    public class LogHelper
    {
        //在网站根目录下创建日志目录(bin文件夹→debug文件夹→logs文件夹)
        public static string path = AppDomain.CurrentDomain.BaseDirectory + "logs";
        //死锁
        public static object loglock = new object();
        public static void Debug(string content)
        {
            WriteLog("DEBUG", content);
        }

        public static void Info(string content)
        {
            WriteLog("INFO", content);
        }

        public static void Error(string content)
        {
            WriteLog("ERROR", content);
        }

        /// <summary>
        /// 系统日志
        /// </summary>
        /// <param name="ex"></param>
        public void Error(Exception ex)
        {
            WriteLog(ex);
        }

        protected static void WriteLog(string type, string content)
        {
            lock (loglock)
            {
                string path1 = path + "\\" + type.ToLower();
                if (!Directory.Exists(path1)) //如果日志目录不存在就创建
                {
                    Directory.CreateDirectory(path1);
                }
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");//获取当前系统时间
                string filename = path1 + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名

                //创建或打开日志文件，向日志文件末尾追加记录
                StreamWriter mySw = File.AppendText(filename);

                //向日志文件写入内容
                string write_content = time + ": " + content;
                mySw.WriteLine(write_content);
                //关闭日志文件
                mySw.Close();
            }
        }

        /// <summary>
        /// 系统日志
        /// </summary>
        /// <param name="ex"></param>
        protected void WriteLog(Exception ex)
        {
            lock (loglock)
            {
                string _path = path + "\\SystemLog\\";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                DateTime now = DateTime.Now;
                string logFileName = now.ToString("yyyy-MM-dd") + ".txt";
                string fullName = path + logFileName;
                string ErrorInfo = "[" + now.ToString("yyyy-MM-dd HH:mm:ss") + "]:" + ex.Message + "\r\nFile:" + ex.StackTrace.Trim();
                using (StreamWriter sr = new StreamWriter(fullName, true, Encoding.Default))
                {
                    sr.WriteLine(ErrorInfo);
                    sr.Close();
                }
            }
        }
    }

    public class LogHelperDynamic
    {
        //在网站根目录下创建日志目录(bin文件夹→debug文件夹→logs文件夹)
        public string path = AppDomain.CurrentDomain.BaseDirectory + "logs";
        //死锁
        public object loglock = new object();
        public void Debug(string content, string subName = "")
        {
            WriteLog("DEBUG", content, subName);
        }

        public void Info(string content, string subName = "")
        {
            WriteLog("INFO", content, subName);
        }

        public void Error(string content, string subName = "")
        {
            WriteLog("ERROR", content,subName);
        }

        /// <summary>
        /// 系统日志
        /// </summary>
        /// <param name="ex"></param>
        public void Error(Exception ex, string subName = "")
        {
            WriteLog(ex, subName);
        }

        protected void WriteLog(string type, string content,string subName="")
        {
            lock (loglock)
            {
                string path1 =string.Empty;
                if (string.IsNullOrEmpty(subName))
                {
                    path1 = path + "\\" + type.ToLower();
                }
                else 
                {
                    path1 = path + "\\" + type.ToLower()+"\\"+ subName.ToLower();
                }
               
                if (!Directory.Exists(path1)) //如果日志目录不存在就创建
                {
                    Directory.CreateDirectory(path1);
                }
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");//获取当前系统时间
                string filename = path1 + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名

                //创建或打开日志文件，向日志文件末尾追加记录
                StreamWriter mySw = File.AppendText(filename);

                //向日志文件写入内容
                string write_content = time + ": " + content;
                mySw.WriteLine(write_content);
                //关闭日志文件
                mySw.Close();
            }
        }

        /// <summary>
        /// 系统日志
        /// </summary>
        /// <param name="ex"></param>
        protected void WriteLog(Exception ex,string subName="")
        {
            lock (loglock)
            {
                string _path =string.Empty;
                if (string.IsNullOrEmpty(subName))
                {
                    _path = path + "\\SystemLog\\";
                }
                else
                {
                    _path = path + "\\" + subName.ToLower();
                }
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                DateTime now = DateTime.Now;
                string logFileName = now.ToString("yyyy-MM-dd") + ".txt";
                string fullName = path + logFileName;
                string ErrorInfo = "[" + now.ToString("yyyy-MM-dd HH:mm:ss") + "]:" + ex.Message + "\r\nFile:" + ex.StackTrace.Trim();
                using (StreamWriter sr = new StreamWriter(fullName, true, Encoding.Default))
                {
                    sr.WriteLine(ErrorInfo);
                    sr.Close();
                }
            }
        }
    }
}